<?php 
    if(isset($_POST['photo'])){
        $userId = $_SESSION['userid'];
        $photo = Photo::withData($_POST['photo']['url'], $_POST['photo']['title'], $_POST['photo']['description'], $_POST['photo']['hexcolor']);
        $photoId = Photo::addPhoto($photo);
        $photoTitle = $_POST['photo']['title'];
        $photoAlias = $photoId.'-'.str_replace(' ', '-', $photoTitle);
        $status = Status::withData($userId, $_POST['photo']['title'], $_POST['photo']['description'], date("Y-m-d"));
        $statusId = Status::addStatus($status);
        //Process hex color
        $rgb = Status::HexToRGB($_POST['photo']['hexcolor']);
        $hexes = array('#990000','#cc0000','#cc3333','#ea4c88','#993399','#663399','#333399','#0066cc','#0099cc','#66cccc','#77cc33','#669900','#336600','#666600','#999900','#cccc33','#ffff00','#ffcc33','#ff9900','#ff6600','#cc6633','#996633','#663300','#000000','#999999','#cccccc');
        $primaryColor = 'undefined';
        foreach($hexes as $hex) {
            $rgb2 = Status::HexToRGB($hex);
            $diff = (abs($rgb['r'] - $rgb2['r']) + abs($rgb['g'] - $rgb2['g']) + abs($rgb['b'] - $rgb2['b']) )/3;
            if ($diff < 30)
                $primaryColor = $hex;
        }
        
        Photo::addPhotoData($photoId, $statusId, $primaryColor);
        $db = Database::getInstance();
        $colors = explode(';', rtrim($_POST['photo']['allcolor'], ';'));
        foreach($colors as $color) {
            $query = "INSERT INTO photocolors VALUES($photoId, $userId, '$color')";
            $db->query($query);
        }
        ?>
        <script>
            setTimeout( function() {
                window.location = '<?php echo $this->baseUrl.'shots/'.$photoAlias; ?>';
            }, 500);
        </script>
    <?php
    } else {
?>
<script>
    function validate(){
        var submit = true;
        var photo_title = document.forms[1].photo_title;
        if(photo_title.value === ""){
            document.forms[1].photo_title.previousSibling.style.color = 'red';
            submit = false;
        }else{
            document.forms[1].photo_title.previousSibling.style.color = 'black';
        }
        var photo_description = document.forms[1].photo_description;
        if(photo_description.value === ""){
            document.forms[1].photo_description.previousSibling.style.color = 'red';
            submit = false;
        }else{
            document.forms[1].photo_description.previousSibling.style.color = 'black';
        }
        $("#photo_hexcolor").attr('value', $('.current').children('a').text());
        if(submit){
            document.forms[1].submit();
        }
    }
    function doWidthChangeMem(elem,startWidth,endWidth,steps,intervals,powr) { 
        //Width changer with Memory by www.hesido.com
            if (elem.widthChangeMemInt)
                window.clearInterval(elem.widthChangeMemInt);
            var actStep = 0;
            elem.widthChangeMemInt = window.setInterval(
                function() { 
                  elem.currentWidth = easeInOut(startWidth, endWidth, steps, actStep, powr);
                  elem.style.width = elem.currentWidth + "px"; 
                  actStep++;
                  if (actStep > steps) window.clearInterval(elem.widthChangeMemInt);
                } 
                ,intervals);
        }
        
    function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
        //Generic Animation Step Value Generator By www.hesido.com 
            var delta = maxValue - minValue; 
            var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
            return Math.ceil(stepp);
    }
    function cancel(){
        document.getElementById('preview').remove();
        document.getElementById("message").style.display = "inherit";
        document.getElementById("upload_photo").style.display = "none";
        document.forms[1].photo_title.value = "";
        document.forms[1].photo_description.value = "";
        document.forms[1].photo_url.value = "";
        doWidthChangeMem(document.getElementById('dropbox-form'), 365, 970, 10, 10, 0.5);
    }
    $(document).on('click', '#color-chips li', function(e){
        $(this).parent('ul').children('li').attr('class', 'color');
        $(this).attr('class', 'color current');
        $("#photo_hexcolor").attr('value', $('.current').children('a').text());
    });
</script>
<div id="dropbox-form">
    <div class="notice success" style="display: inherit;">
        <h2>Upload Images</h2>
    </div>
    <div id="dropbox" style="margin-top: 0px; margin-bottom: 0px; margin-right: 0px; float: left; width: 100%;">
        <span class="message" id="message">Drop images here to upload. <br /><i>(they will only be visible to you)</i></span>
    </div>
 </div>
<form onsubmit="return false;" accept-charset="UTF-8" action="<?php echo $this->baseUrl; ?>Upload" class="account gen-form with-messages" id="upload_photo" method="post" style="float: left; display: none; width: 450px;"> 
    <label for="main_color" style="margin-top: 0px; margin-bottom: 0px;">Main Color</label>
    <div class="colors-explore">
    <ul class="color-chips group" id="color-chips">
    </ul>
    </div>
    <input id="photo_hexcolor" name="photo[hexcolor]" type="hidden" value="">
    <input id="photo_allcolor" name="photo[allcolor]" type="hidden" value="">
    <input id="photo_url" name="photo[url]" type="hidden" value="">
    <div class="form-field">
        <fieldset class="photo_title"><label for="photo_title">Title</label><input id="photo_title" name="photo[title]" size="30" type="text"></fieldset>      
    </div>
    <div class="form-field form-field-bio">
        <fieldset class="photo_description"><label for="photo_description">Description</label><textarea class="bio" cols="40" id="photo_description" name="photo[description]" rows="20" style="resize: none;" maxlength="119"></textarea></fieldset>
    </div>
    <div class="form-btns">
        <input class="form-sub" name="commit" type="submit" value="Upload" onclick="validate();">
        <input alt="Cancel" class="form-btn" id="cancel-avatar" name="commit" style="display: inline;" type="submit" value="Cancel" onclick="cancel();">
    </div>
</form>
<?php } ?>